package com.bignerdranch.android.ecar.Activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.MyImageCache;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class NewsDetailActivity extends BaseActivity implements View.OnClickListener {

    private int position;
    private ImageButton news_detail_back;
    private TextView news_title;
    private ImageView imageView;
    private TextView time_text;
    private ImageView imageView2;
    private TextView visitor;
    private NetworkImageView news_img;
    private WebView content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        initView();
        initData();

    }


    private void initView() {
        news_detail_back = (ImageButton) findViewById(R.id.news_detail_back);
        news_detail_back.setOnClickListener(this);
        news_title = (TextView) findViewById(R.id.news_title);
        news_title.setOnClickListener(this);

        time_text = (TextView) findViewById(R.id.time);
        time_text.setOnClickListener(this);

        visitor = (TextView) findViewById(R.id.visitor);
        visitor.setOnClickListener(this);
        news_img = (NetworkImageView) findViewById(R.id.news_img);
        news_img.setOnClickListener(this);
        content = (WebView) findViewById(R.id.content);
        content.setOnClickListener(this);
    }

    private void initData(){
        position = getIntent().getIntExtra("position", 0);
        RequestUtil.executeJSONObject(this, URLBank.news_url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    JSONObject object = o.getJSONArray("data").getJSONObject(position);


                    SimpleDateFormat sf = new SimpleDateFormat("HH 小时前");
                    Date date = new Date();
                    long time_now = date.getTime();
                    String time_edit = object.getString("edit_time");
                    long time_old = Integer.parseInt(time_edit);
                    long time = time_now - time_old;


                    String title = object.getString("title");
                    String time1 = sf.format(time);
                    String reply_count = object.getString("reply_count");
                    String content_text = object.getString("content");
                    news_title.setText(title);
                    time_text.setText(time1);
                    visitor.setText(reply_count);
                    ImageLoader loader = new ImageLoader(RequestUtil.getRequestQueue(getApplicationContext()),new MyImageCache());
                    news_img.setDefaultImageResId(R.drawable.sample);
                    news_img.setErrorImageResId(R.drawable.good_pressed);
                    news_img.setImageUrl(object.getString("top_image"), loader);
                    content.loadData(content_text, "text/html; charset=UTF-8", null);//解决乱码问题
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.news_detail_back:
                finish();
                break;
        }
    }
}
